
var Water = cc.Node.extend({

    Width:600.0,
    Height:50.0,
    Complexity:40,
    v:[],
    h:[],
    Damping:0.95,
    Viscosity:0.1,
    waterColor:cc.c3b(57,88,121),
    borderColor:cc.c3b(0,0,0),
    opacity:160,
    borderSize:2,
    
    ctor:function (_W, _H, _C, _waterColor, _borderColor) {
        this._super();
        this.scheduleUpdate();
        
        if(_W) this.Width=_W;
        if(_H) this.Height=_H;
        if(_C) this.Complexity=_C;
        if(_waterColor) this.waterColor=_waterColor;
        if(_borderColor) this.borderColor=_borderColor;
        
        this.init();
    },

    init:function () {
        
        for(var i=0; i<this.Complexity; i++)
        {
            this.v[i] = 0.0;
            this.h[i] = ((i%(this.Complexity/5))==0) ? 70.0 : 0.0; //0.0;
        }
    },
    
    getSegementSize:function(){
        return this.Width/this.Complexity;
    },
    
    addForce:function(worldPos, force){
        var localTouch = this.convertToNodeSpace(worldPos);
        var index = Math.round(localTouch.x/this.getSegementSize());
        this.v[index] += force;
    },
    
    handleTouch:function(touch){
        var localTouch = this.convertTouchToNodeSpace(touch);
        localTouch.y = touch.getLocation().y - this.getPosition().y;

        if( localTouch.x > 0 && localTouch.x < this. Width 
            &&    localTouch.y > 0 && localTouch.y < this.Height )
        {
            var index = Math.round(localTouch.x/this.getSegementSize());
            //console.log("Water touched");
            this.v[index] -= 200.0;
        }
    },
    
    handleTouchMove:function(touchLocation){
        
    },
    
    update:function(dt){
    
        for (var i=1; i<this.Complexity-1; i++)
        {
            this.v[i] = this.v[i] + ((this.h[i-1] + this.h[i+1]) / 3.0 - this.h[i]); 
            this.v[i] = this.v[i] * (this.Damping);
        }

        for (var j=1; j<this.Complexity-1; j++)
        {
            this.h[j] += this.v[j] * this.Viscosity;
        }

        this.h[0] = this.h[1];
        this.h[this.Complexity-1] = this.h[this.Complexity-2];
    },
    
    draw:function (ctx) {
       
        var vertices = [];
        
        vertices.push( cc.p( 0.0, 0.0 ) );
        
        var XStep = this.Width / this.Complexity;
        var i=0;
        for(i=0; i<this.Complexity-1; i++)
        {
            vertices.push( cc.p( i*XStep, this.h[i] + this.Height) );
        }
        // push the last out side the for to avoid truncation of X position problem
        vertices.push( cc.p( this.Width, this.h[ this.Complexity-1 ] + this.Height) );
        
        vertices.push( cc.p( this.Width, 0.0 ) );
        
        
        //this is fillText for canvas
        var context = ctx || cc.renderContext;
        context.fillStyle = "rgba(" + this.waterColor.r + "," + this.waterColor.g + "," + this.waterColor.b + "," + this.opacity/255 +")";
        cc.drawingUtil.drawPoly(vertices, this.Complexity, true, true);
        
        context.fillStyle = "rgba(" + this.borderColor.r + "," + this.borderColor.g + "," + this.borderColor.b + "," + this.opacity/255 +")";
        context.lineWidth = this.borderSize;
        cc.drawingUtil.drawPoly(vertices, this.Complexity, true, false);
        
    },
    
});


